<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

session_start();

require_once("database/profile_handler.php");
require_once 'database/message_handler.php';

//    if ($_GET['val'] == 10){

?>
<html>
    <head>
        <script type="text/javascript" src="script/ajax.js"></script>
        <link rel="stylesheet" href="style/style.css" />
    </head>
    <body>
<div id="_title2">Replies :</div><br/>
<?php
    $val = $_GET['val'];
        $m = new message_handler();
        $pr = new profile_handler();
        $gid = $_GET['gid'];
        $msgid = $_GET['msgid'];
        $page = $_GET['page'];

        $m->view_replies($_GET['gid'], $_GET['msgid']);
        $jml_page = ceil((count($m->result[0])) / $val);

          ?><div id="page" align="center">Page :
        <?php

            if ($_GET['page'] == 99) {
                $page = $jml_page;
                $pg = $page;
            }
            else{
                $pg = $jml_page;
            }
            for ($i = $jml_page; $i >= 1; $i-- ){

                ?>
                <a id="headerLink" href="javascript:getMessages(<?php echo $val; ?>,<?php echo $gid;?>,<?php echo $msgid;?>,<?php echo $pg;?>)">
                  <?php echo $i; ?>
                </a>
              <?php
                $pg--;
                echo " ";
              }?>
          </div>

<div id="message_container">
    <br/><br/>
    <div id="message_content">
        <?php
        


        if ($m->result[0] == 0) {
            echo "No replies";
        }
        else {

            //pagination sialan dimulai disini

            //hitung dulu jumlah page yg ada
            ?>
        <div id="page_title" align="center">
            <font color="green">Page <?php echo $page; ?></font>
        </div>
            <?php
            $jml_record = (count($m->result[0])) - 1;
            
            //echo $jml_page. "<br/>";

            //tentukan indeks pertama (pada tiap halaman, hal 1 = last, hal 2 = last - val, hal 3 = last - val*2)
            //tentukan indeks terakhir (pada tiap halaman, hal 1 = start-(val*1)+1, hal 2 = start-(val*2)+1, hal 3 = last - val*2)

            //pej terakhir dulu aja, ntar tinggal di-for

            for ($j = $jml_page; $j >= 1; $j-- ) {

            $kali = $jml_page - $j;

            $idxstart = $jml_record - ($val*$kali);
           // echo "start = ". $idxstart. "; ";
            $idxend = $idxstart - $val + 1;
            if ($idxend < 0) $idxend = 0;
           // echo "end = ". $idxend. "<br/>";
            $i = 0;
                for ($k = $idxstart; $k >= $idxend; $k--) {

                    //if ($m->result[$i] != null){
                    $pr->get_nama_lengkap($m->result[0][$k]);
                    if ($m->result[0][$k] == $_SESSION['Username']) {
                        //simpen dulu di string, ntar tinggal di-view
                        $name_str[$j][$i] = "<a href='otherprofile.php?UserTujuan=". $m->result[0][$k]. "' id='link_name'>". "You" . "</a><br/>";
                    }
                    else {
                        $name_str[$j][$i] = "<a href='otherprofile.php?UserTujuan=". $m->result[0][$k]. "' id='link_name'>". $pr->result[0] . "</a><br/>";
                    }

                    $reply_str[$j][$i] = "<div id='reply_body'><div id='reply_text'>". $m->result[1][$k] . "</div><br/></div><br/>";
                    // }
                    $i++;

                }

            }
            
                
           // }
            echo "</div><br/>";
            for ($i = 0; $i < count($name_str[$jml_page]); $i++) {
                //coba page terakhir dulu gan
                if ($name_str[$page][$i] != null){
                    echo "by ";
                    echo $name_str[$page][$i];
                    echo $reply_str[$page][$i];
                }
            
            }
        }

        ?>
    </div>
</div>
    </body>
</html>
<?php
//  }
?>

